package org.toycode.logindemo.application.mock;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.toycode.logindemo.entity.Article;
import org.toycode.logindemo.entity.Paper;
import org.toycode.logindemo.entity.Task;

/**
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.1, Sep 1, 2009
 */
final public class ClientMockDataGenerator {

    private static ClientMockDataGenerator instance;
    private List<Task> tasks;
    private List<Article> articles;
    private List<Paper> papers;
    private static final int ARTICLE_GEN_SIZE = 1000;
    private static final int TASK_GEN_SIZE = 500;
    private static final int PAPER_GEN_SIZE = 500;

    public static synchronized ClientMockDataGenerator getInstance() {
        if (instance == null) {
            instance = new ClientMockDataGenerator();
        }

        return instance;
    }

    public int getTasksSize() {
        return tasks.size();
    }

    public int getArticlesSize() {
        return articles.size();
    }

    public int getPapersSize() {
        return papers.size();
    }

    public List<Paper> getPapers(final int start, final int rows) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ClientMockDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        return papers.subList(start, start + rows);
    }

    public List<Article> getArticles(final int start, final int rows) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ClientMockDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        return articles.subList(start, start + rows);
    }

    public List<Task> getTasks(final int start, final int rows) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ClientMockDataGenerator.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
        return tasks.subList(start, start + rows);
    }

    private void mockTasks() {
        for (int i = 0; i < TASK_GEN_SIZE; i++) {
            final Task task = new Task();
            task.setId(new Long(i));
            task.setOwner("owner " + i);
            task.setDescription("description " + i);
            task.setProgressDescription("progress description " + i);

            tasks.add(task);
        }
    }

    private void mockArticles() {
        for (int i = 0; i < ARTICLE_GEN_SIZE; i++) {
            final Article article = new Article();
            article.setId(new Long(i));
            article.setAuthor("author " + i);
            article.setPublishDate(new Date());

            articles.add(article);
        }
    }

    private void mockPapers() {
        for (int i = 0; i < PAPER_GEN_SIZE; i++) {
            final Paper paper = new Paper();
            paper.setId(new Long(i));
            paper.setAuthor("author " + i);
            paper.setPublishDate(new Date());
            paper.setScore(i * i);
            paper.setStatus("status " + i);
            paper.setSummary("summary " + i);
            paper.setTitle("title " + i);

            papers.add(paper);
        }
    }

    private ClientMockDataGenerator() {
        tasks = new ArrayList<Task>();
        articles = new ArrayList<Article>();
        papers = new ArrayList<Paper>();

        mockPapers();
        mockTasks();
        mockArticles();
    }
}
